import { request } from './request'

export interface IGetUserListByOrgIdParams {
  pageNum: number
  pageSize: number
  orgId: string | number
  accountName: string
}

export function getUserListByOrgId(params: IGetUserListByOrgIdParams) {
  const { orgId, pageNum = 1, pageSize = 10 } = params
  const conditions = { orgId: `NumberLong(${orgId})` }
  if (params.accountName) {
    conditions.accountName = { $regex: params.accountName }
  }
  // conditions
  // this.conditions['data.name'] = { $regex: val }
  return request.post('/user/list', {
    conditions,
    fields: { orgName: 1, orgId: 1, roleIds: 1, isWxInit: 1 },
    // sort: {},
    pageInfo: {
      pageNum,
      pageSize,
    },
    // templateName: 'c_sys_user',
  })
}

export function getUserDetail(accountId: string | number) {
  return request.get(`/user/detail/${accountId}`)
}

export interface IUpsertUserParams {
  account: string
  accountName: string
  email: string
  gender?: string
  iconUrl: string | any[]
  id?: string
  password: string
  phone: string
  position: string
  roleIds: number[] | string[]
  wechat: string
  orgId: string
  orgName: string
}

export function upsertUser(params: IUpsertUserParams) {
  return request.put('/user/upsert', params)
}

export function deleteUser(accountId: string | number) {
  return request.delete(`/user/delete/${accountId}`)
}

export function getCurrentUser() {
  return request.get('/user/currentUser')
}
